
#include <Inklude/ikCUdpClient.h>
#include <Inklude/Internal/ikCUdpBoost.h>
#include <Inklude/Inklude.h>

using namespace Inklude;
using namespace Inklude::Internal;

ikCUdpClient::ikCUdpClient()
{
}

void ikCUdpClient::Initialize(unsigned int nRecvPort)
{
    m_pUdp.reset(new ikCUdpBoost);
    m_pUdp->Initialize(nRecvPort);
}

void ikCUdpClient::SetDestinationAddress(const char* IPAddress, unsigned int nPort)
{
    m_pUdp->SetDestinationAddress(IPAddress, nPort);
}


size_t ikCUdpClient::Send(void* pBuffer, size_t nDataSize)
{
    return m_pUdp->SyncSend(pBuffer, nDataSize);
}

size_t ikCUdpClient::Receive(void* pBuffer, size_t nDataSize)
{
    return m_pUdp->SyncReceive(pBuffer, nDataSize);
}

void ikCUdpClient::AsyncSend(void* pBuffer, size_t nDataSize, AsyncHandler endHandler)
{
    assert(endHandler != NULL && "You should provide AsyncHandler for ASync routine");
    assert( !(endHandler._Expired()) && "Given AsyncHandler already has been expired");
    m_pUdp->AsyncSend(pBuffer, nDataSize, endHandler);
    m_pUdp->Run();
}

void ikCUdpClient::AsyncReceive(void* pBuffer, size_t nDataSize, AsyncHandler endHandler)
{
    assert(endHandler != NULL && "You should provide AsyncHandler for ASync routine");
    assert( !(endHandler._Expired()) && "Given AsyncHandler already has been expired");
    m_pUdp->AsyncReceive(pBuffer, nDataSize, endHandler);
    m_pUdp->Run();
}

void ikCUdpClient::_init()
{
}


// ---------------------------------------------------------------
// --- ASync Routine
// ---------------------------------------------------------------


void ikCUdpClientAsync::Initialize(unsigned int nRecvPort)
{
    m_pUdp->Initialize(nRecvPort);
}

void ikCUdpClientAsync::SetDestinationAddress(const char* IPAddress, unsigned int nPort)
{
    m_pUdp->SetDestinationAddress(IPAddress, nPort);
}

size_t ikCUdpClientAsync::Send(const void* pBuffer, size_t nDataSize, AsyncHandler endHandler) 
{
    assert(endHandler == NULL && "You should not put AsyncHandler for Sync routine");
    m_pUdp->AsyncSend(const_cast<void*>(pBuffer), nDataSize, endHandler);
    return 0;
}

size_t ikCUdpClientAsync::Receive(void* pBuffer, const size_t nDataSize, AsyncHandler endHandler) 
{
    assert(endHandler == NULL && "You should not put AsyncHandler for Sync routine");
    m_pUdp->AsyncReceive(pBuffer, nDataSize, endHandler);
    return 0;
}



// ---------------------------------------------------------------
// --- Sync Routine
// ---------------------------------------------------------------




void ikCUdpClientSync::Initialize(unsigned int nRecvPort)
{
    m_pUdp->Initialize(nRecvPort);
}

void ikCUdpClientSync::SetDestinationAddress(const char* IPAddress, unsigned int nPort)
{
    m_pUdp->SetDestinationAddress(IPAddress, nPort);
}

size_t ikCUdpClientSync::Send(const void* pBuffer, size_t nDataSize, AsyncHandler endHandler)
{
    return m_pUdp->Send(const_cast<void*>(pBuffer), nDataSize);
}

size_t ikCUdpClientSync::Receive(void* pBuffer, const size_t nDataSize, AsyncHandler endHandler) 
{
    return m_pUdp->Receive(pBuffer, nDataSize);
}